//
//  YMMineViewController.m
//  YMMedical
//
//  Created by y s on 2023/6/19.
//

#import "YMMineViewController.h"

@interface YMMineViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tvMain;

@property (nonatomic, strong) NSArray *arrList;

@property (nonatomic, assign) NSInteger headTapNum;
@property (nonatomic, copy) NSString *headTapStr;

@end

@implementation YMMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self addNotify];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.isWhiteStatusBar = YES;
    [self loadData];
}

#pragma mark -- UI
- (void)setUI{
    self.navBarHidden = YES;
    [self updateLanguage];
    [self.view addSubview:self.tvMain];
    [self.tvMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
- (void)addNotify{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadData) name:LoginedNoti_key object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLanguage) name:kLanguage_change object:nil];
}

- (void)loadData{
    [self.tvMain reloadData];
}
- (void)updateLanguage{
    self.arrList = @[@{@"title":KLanguage(@"operate_book_txt"), @"icon":@"mine_icon_instructions", @"type":@2},
                     @{@"title":KLanguage(@"tester_man_txt"), @"icon":@"mine_icon_beiceren", @"type":@3},
                     @{@"title":KLanguage(@"questions_txt"), @"icon":@"mine_icon_problem", @"type":@4},
                     @{@"title":KLanguage(@"feedback_txt"), @"icon":@"mine_icon_fankui", @"type":@5},
                     @{@"title":KLanguage(@"link_server_txt"), @"icon":@"mine_icon_kefu", @"type":@6}];
    [self.tvMain reloadData];
}


#pragma mark -- UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.arrList.count+1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0){
        YMMineTopInforAndDeviceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YMMineTopInforAndDeviceCell" forIndexPath:indexPath];
        [cell updateData: [YMMyModel shared].userName];
        WEAK_SELF;
        cell.tapClickBlock = ^{
            weakSelf.headTapNum++;
            if (weakSelf.headTapNum%5 == 0){
                if (weakSelf.headTapStr != nil && [weakSelf.headTapStr isEqualToString:@"显示"]){
                    //隐藏
                    [YMMyModel shared].showCameraTips = NO;
                    [SHUD showInfo:@"调参信息已切换至隐藏"];
                }else{
                    //显示
                    [YMMyModel shared].showCameraTips = YES;
                    [SHUD showInfo:@"调参信息已切换至显示"];
                }
            }
        };
        return cell;
    }else{
        YMMineHomeListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YMMineHomeListCell" forIndexPath:indexPath];
        if (indexPath.section - 1 < self.arrList.count){
            [cell updateData:[SDataUtil dictOf:self.arrList[indexPath.section-1]]];
        }
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0){
        return [YMMineTopInforAndDeviceCell getCellHeight];
    }else{
        return [YMMineHomeListCell getCellHeight];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return [SFitUtil fit:15];
}
#pragma mark -- setter and getter
- (UITableView *)tvMain{
    if (!_tvMain) {
        _tvMain = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tvMain.backgroundColor = [UIColor clearColor];
        _tvMain.delegate   = self;
        _tvMain.dataSource = self;
        _tvMain.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tvMain.showsVerticalScrollIndicator   = NO;
        _tvMain.showsHorizontalScrollIndicator = NO;
        _tvMain.estimatedRowHeight = 0;
        _tvMain.estimatedSectionHeaderHeight = 0;
        _tvMain.estimatedSectionFooterHeight = 0;
        _tvMain.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [_tvMain registerClass:[YMMineTopInforAndDeviceCell class] forCellReuseIdentifier:@"YMMineTopInforAndDeviceCell"];
        [_tvMain registerClass:[YMMineHomeListCell class] forCellReuseIdentifier:@"YMMineHomeListCell"];
    }
    return _tvMain;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
